package mest.service.LGF.FinancialPurchaseMoney.Impl;

import mest.VO.LGF.FinancialPurchaseMoney.financial_PurchaseMoneyRecordVO;
import mest.dao.LGF.FinancialPurchaseMoney.IfinancialPurchaseMoneyDao;
import mest.service.LGF.FinancialPurchaseMoney.IFinancialPurchaseMoneyService;
import mest.service.system.DBService;
import mest.util.mestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-12-19 18:01
 */
@Service
public class FinancialPurchaseMoneyServiceImpl implements IFinancialPurchaseMoneyService {

    @Resource
    HttpServletRequest request;
//    String theirDb = mestUtil.getCompanyDB(request);


    @Resource
    private IfinancialPurchaseMoneyDao ifpmd;
    @Override
    public Map baseSelectNofinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ifpmd.countNOFinish(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ifpmd.selectAllNOFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectFinish(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ifpmd.countFinish(theirDb);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ifpmd.selectAllFinish(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectSubjectType(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ifpmd.countSubjectType();

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ifpmd.selectAllSubjectType(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public Map baseSelectAccountFile(int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ifpmd.countAccountFile();

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ifpmd.selectAllAccountFile(theirDb,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public int addFinancialRecord(financial_PurchaseMoneyRecordVO fpmrVO) {
        String theirDb = mestUtil.getCompanyDB(request);
        fpmrVO.setTheirDb(theirDb);
        int i =-2;
        int i1 =-2;
        int i2 =-2;
        int i3 = -2;
        try {
             i = ifpmd.addReocrd(fpmrVO);
             i1 = ifpmd.updatePlanHasBeenMoney(fpmrVO);
             i2 = ifpmd.updateFinancialHasBeenMoney(fpmrVO);

             //根据acfId和thisMoney修改account_File上面的余额
            i3 = ifpmd.updateAccountFileBanlanceByACF(fpmrVO);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(FinancialPurchaseMoneyServiceImpl.class.getName());
            System.out.println(e.getClass().getName());
        }

        if(i ==-1 || i1 == -1 || i2 == -1 || i3 ==-1)
            return -1;

        return i;
    }



    @Override
    public Map selectPastRecord(int fpmId, int index, int pageSize) {
        String theirDb = mestUtil.getCompanyDB(request);
        Map map = new HashMap();
        int count = ifpmd.selectPastRecordCount();

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",ifpmd.selectPastRecord(theirDb,fpmId,(index-1)*pageSize,pageSize));
        return map;
    }

    @Override
    public int updateByFPMId(int fpmId, int status) {
        String theirDb = mestUtil.getCompanyDB(request);
        int i =-2;
        try {
            i = ifpmd.updateByFPMId(theirDb,fpmId,status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }
}
